package com.wish.group05.finance.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.group05.base.entity.CwReceiptPlan;
import com.wish.group05.base.service.ICwReceiptPlanService;
import com.wish.group05.finance.dto.CwReceiptPlanDto;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.group05.common.log.annotation.Log;
import com.wish.group05.common.log.enums.BusinessType;
import com.wish.group05.common.security.annotation.PreAuthorize;
import com.wish.group05.common.core.web.controller.BaseController;
import com.wish.group05.common.core.web.domain.AjaxResult;
import com.wish.group05.common.core.utils.poi.ExcelUtil;
import com.wish.group05.common.core.web.page.TableDataInfo;

/**
 * 收款计划Controller
 * 
 * @author wish
 * @date 2022-05-26
 */
@RestController
@RequestMapping("/plan")
public class TCwReceiptPlanController extends BaseController
{
    @Autowired
    private ICwReceiptPlanService tCwReceiptPlanService;

    @GetMapping("getMoney")
    public AjaxResult getMoney() {
        HashMap<String, Object> map = new HashMap<>();

        List<CwReceiptPlan> list = tCwReceiptPlanService.list();
        //应收金额
        //应收金额
        BigDecimal amountReceivable = new BigDecimal(0);
        //实收金额
        BigDecimal amountCollected = new BigDecimal(0);

        for (CwReceiptPlan cwReceiptPlan : list) {
            if(cwReceiptPlan.getPaymentAmount() != null) {
                amountReceivable = amountReceivable.add(cwReceiptPlan.getPaymentAmount());
//				System.out.println("应收金额："+amountReceivable);
            }
            //收款状态已收款
            if(cwReceiptPlan.getSkStatus() != null && cwReceiptPlan.getSkStatus() == 2) {
                if(cwReceiptPlan.getPaymentAmount() != null) {
                    amountCollected = amountCollected.add(cwReceiptPlan.getPaymentAmount());
//					System.out.println("实收金额："+amountCollected);
                }
            }
        }
        System.out.println("应收金额共计："+amountReceivable);
        System.out.println("实收金额共计："+amountCollected);
        map.put("amountReceivable",amountCollected);
        map.put("amountCollected",amountCollected);

        return AjaxResult.success(map);
    }

    /**
     * 查询收款计划列表
     */
    @PreAuthorize(hasPermi = "finance:plan:list")
    @GetMapping("/list")
    public TableDataInfo list(CwReceiptPlanDto cwReceiptPlanDto)
    {
        HashMap<String, Object> map = new HashMap<>();

        QueryWrapper<CwReceiptPlan> wrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(cwReceiptPlanDto.getContractNo())) {
            wrapper.like("contract_no",cwReceiptPlanDto.getContractNo());
        }
        if (cwReceiptPlanDto.getCollectionDate() != null) {
            wrapper.ge("collection_date",cwReceiptPlanDto.getCollectionDate());
        }
        if (cwReceiptPlanDto.getCollectionDate1() != null) {
            wrapper.le("collection_date",cwReceiptPlanDto.getCollectionDate1());
        }
        if (cwReceiptPlanDto.getSkStatus() != null) {
            wrapper.eq("sk_status",cwReceiptPlanDto.getSkStatus());
        }
        if (cwReceiptPlanDto.getRealCollectionDate() != null) {
            wrapper.ge("real_collection_date",cwReceiptPlanDto.getRealCollectionDate());
        }
        if (cwReceiptPlanDto.getRealCollectionDate1() != null) {
            wrapper.le("real_collection_date",cwReceiptPlanDto.getRealCollectionDate1());
        }
        if (cwReceiptPlanDto.getBillType() != null) {
            wrapper.eq("bill_type",cwReceiptPlanDto.getBillType());
        }
        wrapper.eq("receipt_type",cwReceiptPlanDto.getReceiptType());
        startPage();
        List<CwReceiptPlan> list = tCwReceiptPlanService.list(wrapper);
        return getDataTable(list);
    }

//    /**
//     * 导出收款计划列表
//     */
//    @PreAuthorize(hasPermi = "system:plan:export")
//    @Log(title = "收款计划", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, TCwReceiptPlan tCwReceiptPlan) throws IOException
//    {
//        List<TCwReceiptPlan> list = tCwReceiptPlanService.selectTCwReceiptPlanList(tCwReceiptPlan);
//        ExcelUtil<TCwReceiptPlan> util = new ExcelUtil<TCwReceiptPlan>(TCwReceiptPlan.class);
//        util.exportExcel(response, list, "plan");
//    }
//
//    /**
//     * 获取收款计划详细信息
//     */
//    @PreAuthorize(hasPermi = "system:plan:query")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return AjaxResult.success(tCwReceiptPlanService.selectTCwReceiptPlanById(id));
//    }
//
//    /**
//     * 新增收款计划
//     */
//    @PreAuthorize(hasPermi = "system:plan:add")
//    @Log(title = "收款计划", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody TCwReceiptPlan tCwReceiptPlan)
//    {
//        return toAjax(tCwReceiptPlanService.insertTCwReceiptPlan(tCwReceiptPlan));
//    }
//
//    /**
//     * 修改收款计划
//     */
//    @PreAuthorize(hasPermi = "system:plan:edit")
//    @Log(title = "收款计划", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody TCwReceiptPlan tCwReceiptPlan)
//    {
//        return toAjax(tCwReceiptPlanService.updateTCwReceiptPlan(tCwReceiptPlan));
//    }
//
//    /**
//     * 删除收款计划
//     */
//    @PreAuthorize(hasPermi = "system:plan:remove")
//    @Log(title = "收款计划", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(tCwReceiptPlanService.deleteTCwReceiptPlanByIds(ids));
//    }
}
